import {ElMessage, ElMessageBox, ElNotification} from "element-plus";
import {isBoolean} from "lodash-es";
import {MessageBoxOptions, MessageOptions, MessageType, NotificationOptions} from "@/components/Notice/types";

/* ==================== ElMessage ==================== */
export function useMessage(message: string, type?: MessageType, options?: MessageOptions) {
    ElMessage({
        message: message,
        icon: options?.icon,
        type: type || "success",
        center: options?.center || true,
        duration: options?.duration || 2000,
        grouping: isBoolean(options?.grouping) ? options?.grouping : true,
        showClose: options?.showClose,
    })
}


/* ==================== ElNotification ==================== */
export function useNotice(msg: string, options?: NotificationOptions) {
    ElNotification({
        message: msg,
        title: options?.title,
        type: options?.type || "error",
        icon: options?.icon,
        offset: options?.offset,
        showClose: options?.showClose,
        duration: options?.duration || 2000,
        dangerouslyUseHTMLString: options?.htmlString
    })
}

/* ==================== ElMessageBox ==================== */
export function useInform(msg: string, callback: Function, options?: MessageBoxOptions) {
    ElMessageBox.alert(
            msg,
            options?.title,
            {
                type: options?.type || "warning",
                confirmButtonText: options?.confirmText,
                draggable: options?.draggable,
                callback: () => {
                    callback()
                }
            }
    ).catch(() => {
        callback();
    })
}

export function useConfirm(msg: string, okCallBack?: Function, options?: MessageBoxOptions) {
    ElMessageBox.confirm(
            msg,
            options?.title,
            {
                type: options?.type || "warning",
                icon: options?.icon,
                confirmButtonText: options?.confirmText,
                cancelButtonText: options?.cancelText,
                center: options?.center,
                draggable: options?.draggable,
                autofocus: options?.autofocus,
                showClose: options?.showClose,
                beforeClose: (action, instance, done) => {
                    if (options?.beforeClose) {
                        if (action === 'confirm') {
                            instance.confirmButtonLoading = true;
                            instance.confirmButtonText = options?.progressText || "执行中";

                            setTimeout(() => {
                                try {
                                    options.beforeClose && options.beforeClose();
                                    done();
                                } catch (e) {
                                    instance.confirmButtonLoading = false;
                                    instance.confirmButtonText = <string>options?.confirmText;
                                }
                            }, options?.duration || 500)
                        } else {
                            done();
                        }
                    } else {
                        done();
                    }
                }
            }
    ).then(() => {
        okCallBack && okCallBack();
    }).catch(() => {
        options?.cancelCb && options.cancelCb();
    })
}
